﻿// © 2009 Microsoft Corporation.  All rights reserved.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.Security.Application;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Web.UI.HtmlControls;

namespace Microsoft.InformationSecurity.RiskManagement.ISRMWeb.UserControls
{
    public partial class HtmlEditorControl : System.Web.UI.UserControl
    {
        #region Local Variables

        /// <summary>
        /// textEditorDefault control.
        /// </summary>
        /// <remarks>
        /// Auto-generated field.
        /// To modify move field declaration from designer file to code-behind file.
        /// </remarks>
        public global::System.Web.UI.HtmlControls.HtmlTextArea textEditorDefault;

        int htmlEditorFrameWidth = 300;
        int htmlEditorFrameHeight = 300;
        string imagesVirtualPath = "";
        Color modalDialogBackColor= Color.LightGray;
        Color toolbarBackColor = Color.White;
        Color toolbarSelectionColor = Color.Khaki;
        Color editorBackColor = Color.White;
        const string defaultHtml = "<p>You can edit this</p>";
        #endregion

        #region Public Properties
        [Description("Returns HTML or Text based on the tab selected in the editor")]
        [Browsable(true)]
        public string Text
        {
            get
            {
                string returnHtml = "";
                if (this.SelectedEditorTab.ToLower() == "html")
                {
                    if (ContentHtml.Value == "")
                        returnHtml = defaultHtml;
                    else
                    {
                        returnHtml = ContentHtml.Value;
                    }
                }
                else
                {
                    returnHtml = textEditorDefault.Value;
                }
                return returnHtml;

            }
            set
            {
                ContentHtml.Value = value;
            }
        }

        [Description("Width of the HTML editor")]
        [Browsable(true)]
        public int Width
        {
            set
            {
                htmlEditorFrameWidth = value;
                ViewState["htmlEditorFrameWidth"] = value;
            }
            get
            {
                return htmlEditorFrameWidth;
            }
        }

        [Description("Height of the HTML editor")]
        [Browsable(true)]
        public int Height
        {
            set
            {
                htmlEditorFrameHeight = value;
                ViewState["htmlEditorFrameHeight"] = value;
            }
            get
            {
                return htmlEditorFrameHeight;
            }
        }

        [Description("Virtual path to load the images from")]
        [Browsable(true)]
        public string ImagesVirtualPath
        {
            set
            {
                imagesVirtualPath = value;
                ViewState["imagesVirtualPath"] = value;
                
            }
            get
            {
                return imagesVirtualPath;
            }
        }

        [Description("Background Color of the Modal Dialog displayed for selecting images.")]
        [Browsable(true)]
        public Color ModalDialogBackColor
        {
            set
            {
                if (value != null)
                {
                    modalDialogBackColor = value;
                    ViewState["modalDialogBackColor"] = value;
                    
                }
            }
            get
            {
                return modalDialogBackColor;
            }
        }

        [Description("Background Color of the toolbar buttons.")]
        [Browsable(true)]
        public Color ToolbarBackColor
        {
            set
            {
                if (value != null)
                {
                    toolbarBackColor = value;
                    ViewState["toolbarBackColor"] = value;
                    
                }
            }
            get
            {
                return toolbarBackColor;
            }
        }

        [Description("Selection Color of the toolbar buttons.")]
        [Browsable(true)]
        public Color ToolbarSelectionColor
        {
            set
            {
                if (value != null)
                {
                    toolbarSelectionColor = value;
                    ViewState["toolbarSelectionColor"] = value;
                    
                }
            }
            get
            {
                return toolbarSelectionColor;
            }
        }

        [Description("Background Color of the HTML editor.")]
        [Browsable(true)]
        public Color EditorBackColor
        {
            set
            {
                if (value != null)
                {
                    editorBackColor = value;
                    ViewState["editorBackColor"] = value;
                }
            }
            get
            {
                return editorBackColor;
            }
        }

        [Description("Read only property that returns selected editor tab.")]
        [Browsable(true)]
        public string SelectedEditorTab
        {
            get
            {
                return TabContainer1.Tabs[TabContainer1.ActiveTabIndex].HeaderText;
            }
            set 
            {
                if (value.ToLower() == "html")
                    TabContainer1.ActiveTabIndex = 0;
                else
                    TabContainer1.ActiveTabIndex = 1;
            }
        }

        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            textEditorDefault.Attributes.Add("onload", "setTextFromHtml();");
            if (ViewState["htmlEditorFrameWidth"] != null)
                htmlEditorFrameWidth = (int)ViewState["htmlEditorFrameWidth"];
            if (ViewState["htmlEditorFrameHeight"] != null)
                htmlEditorFrameHeight = (int)ViewState["htmlEditorFrameHeight"];
            if (ViewState["imagesVirtualPath"] != null)
                imagesVirtualPath = (string)ViewState["imagesVirtualPath"];
            if (ViewState["modalDialogBackColor"] != null)
                modalDialogBackColor = (Color)ViewState["modalDialogBackColor"];
            if (ViewState["toolbarBackColor"] != null)
                toolbarBackColor = (Color)ViewState["toolbarBackColor"];
            if (ViewState["toolbarSelectionColor"] != null)
                toolbarSelectionColor = (Color)ViewState["toolbarSelectionColor"];
            if (ViewState["editorBackColor"] != null)
                editorBackColor = (Color)ViewState["editorBackColor"];

            ddeFonts.DropArrowBackColor = toolbarSelectionColor;
            ddeFonts.HighlightBorderColor = toolbarSelectionColor;
            ddeFontSizes.DropArrowBackColor = toolbarSelectionColor;
            ddeFontSizes.HighlightBorderColor = toolbarSelectionColor;
            ddeTextColor.DropArrowBackColor = toolbarSelectionColor;
            ddeTextColor.HighlightBorderColor = toolbarSelectionColor;
            ddeBackColor.DropArrowBackColor = toolbarSelectionColor;
            ddeBackColor.HighlightBorderColor = toolbarSelectionColor;
            pnlFonts.BackColor = toolbarBackColor;
            pnlFontSizes.BackColor = toolbarBackColor;
            pnlBackColor.BackColor = toolbarBackColor;
            pnlFonts.BackColor = toolbarBackColor;
            pnlModal.BackColor = modalDialogBackColor;
            TabContainer1.Width = this.Width + 35;
            TabContainer1.Height = this.Height + 42;
            LoadImages();
        }

        

        private void LoadImages() 
        {
            string physicalPath = Server.MapPath(imagesVirtualPath);
            pnlImages.Controls.Clear();
            if (Directory.Exists(physicalPath)) 
            {
                List<string> imgFiles = new List<string>();
                imgFiles.AddRange(Directory.GetFiles(physicalPath,"*.jpg",SearchOption.TopDirectoryOnly));
                imgFiles.AddRange(Directory.GetFiles(physicalPath, "*.gif", SearchOption.TopDirectoryOnly));
                imgFiles.AddRange(Directory.GetFiles(physicalPath, "*.png", SearchOption.TopDirectoryOnly));
                imgFiles.Sort();
                HtmlTable tblImages = new HtmlTable();
                tblImages.Border = 0;
                tblImages.CellPadding = 2;
                tblImages.CellSpacing = 2;
                tblImages.Attributes.Add("unselectable", "on");
                
                for (int i=0;i<imgFiles.Count;i++)
                {
                    if (i % 6 == 0)
                    {
                        tblImages.Rows.Add(new HtmlTableRow());
                        tblImages.Rows[tblImages.Rows.Count-1].Attributes.Add("unselectable", "on");
                    }
                    HtmlTableCell cell = new HtmlTableCell("td");
                    cell.Attributes.Add("unselectable", "on");
                    HtmlImage img = new HtmlImage();
                    img.Src = imagesVirtualPath + "/" + Path.GetFileName(imgFiles[i]);
                    img.Style.Add(HtmlTextWriterStyle.Cursor, "pointer");
                    img.Border = 1;
                    img.Width = 55;

                    img.Attributes.Add("onclick", "changeImageBorder(this);imageSrc = " + AntiXss.JavaScriptEncode(Request.Url.Scheme) + "+" + AntiXss.JavaScriptEncode(Uri.SchemeDelimiter) + "+" + AntiXss.JavaScriptEncode(Request.Url.Authority) + "+'/'+" + AntiXss.JavaScriptEncode(img.Src) + ";");

                    cell.Controls.Add(img);
                    tblImages.Rows[tblImages.Rows.Count-1].Cells.Add(cell);
                }
                pnlImages.Controls.Add(tblImages);
            }
        }
    }
}